package service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import model.IOrder;
import model.Order;
import persistence.dao.DaoManager;
import persistence.dao.OrderDao;
import persistence.dao.base.ConnectionBehavior;
import persistence.dao.base.DaoCommand;
import persistence.entity.OrderEntity;
import service.OrderService;

public class OrderServiceImpl implements OrderService {

	private DaoManager daoManager;

	public OrderServiceImpl(DaoManager daoManager) {
		this.daoManager = daoManager;
	}

	protected DaoManager getDaoManager() {
		return daoManager;
	}

	@Override
	public IOrder delete(final int[] id_check_order) {
		return (IOrder) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final OrderDao orderDao = manager.getDao(OrderDao.class);
				orderDao.delete(id_check_order);
				return null; 
			}
		}, ConnectionBehavior.CLOSE);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<IOrder> getOrder() {
		return (List<IOrder>) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final OrderDao orderDao = manager.getDao(OrderDao.class);
				final List<OrderEntity> listOrderEntity = orderDao.getOrder();
				final List<IOrder> listOrder = new ArrayList<IOrder>();
				for (final OrderEntity orderEntity : listOrderEntity) {
					listOrder.add(convertToModel(orderEntity));
				}
				return listOrder;
			}
		}, ConnectionBehavior.CLOSE);
	}

	private IOrder convertToModel(OrderEntity orderEntity) {
		IOrder order = null;
		if (orderEntity != null) {
			order = new Order(orderEntity.getId_order(),
					orderEntity.getCityFrom(), orderEntity.getCityTo(),
					orderEntity.getId_routs(), orderEntity.getName_order(),
					orderEntity.getDate_order(), orderEntity.getDate_routs(),
					orderEntity.getName_city(), orderEntity.getName_routs());
		}
		return order;
	}

	@Override
	public List<IOrder> getOrder(final int id_routs, final String date1,
			final String date2, final String date3) {
		return (List<IOrder>) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final OrderDao orderDao = manager.getDao(OrderDao.class);
				final List<OrderEntity> listOrderEntity = orderDao.getOrder(id_routs, date1, date2, date3);
				final List<IOrder> listOrder = new ArrayList<IOrder>();
				for (final OrderEntity orderEntity : listOrderEntity) {
					listOrder.add(convertToModel(orderEntity));
				}
				return listOrder;
			}
		}, ConnectionBehavior.CLOSE);
	}

	@Override
	public IOrder setOrder(final int cityFrom, final int cityTo,
			final int id_routs, final String names, final String date_routs,
			final UUID id_ticket) {
		return (IOrder) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final OrderDao orderDao = manager.getDao(OrderDao.class);
				orderDao.setOrder(cityFrom, cityTo, id_routs, names,
						date_routs, id_ticket);
				return null; 
			}
		}, ConnectionBehavior.CLOSE);
	}
}
